package com.software.levelupandroid.taskIssue.view.label;

import android.content.Context;

import androidx.annotation.NonNull;

import com.software.levelupandroid.entity.Label;
import com.software.levelupandroid.entity.Result;
import com.software.levelupandroid.home.CallBack;
import com.software.levelupandroid.utils.JsonUtil;
import com.software.levelupandroid.utils.OkhttpUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;


public class LabelRepository {
    private static LabelRepository labelRepository;
    private List<Label> labelList = new ArrayList<>();

    public static LabelRepository getLabelRepository(){
        if (labelRepository == null){
            labelRepository = new LabelRepository();
        }
        return labelRepository;
    }

    /**
     * 获取label
     * @param callBack

     */
    public void getLabelByPriority(CallBack<List<Label>> callBack, Context context){
        Call call = OkhttpUtil.getRequest("label/getAllLabels", context);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                callBack.onFailure(e.getMessage());
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                String resp = response.body().string();
                Result<List<Label>> result = JsonUtil.convertJsonToLabelList(resp);

                if (result != null){
                    if (result.getCode() != null && result.getCode().equals("200")){
                        List<Label> label = result.getData();
                        callBack.onSuccess(label);
                    }else {
                        callBack.onFailure("result.getCode==null||result.getCode.isNotEquals('200'): 获取任务失败");
                    }
                }else {
                    callBack.onFailure("result==null: 未成功获取数据");
                }
            }
        });
    }
}
